/*
 * This file is part of TradeHelper library.
 *
 * TradeHelper library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TradeHelper library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TradeHelper library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rag.core;

import com.alee.utils.TextUtils;
import com.rag.managers.ForumManager;
import com.rag.managers.UsersManager;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

import javax.swing.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * User: mgarin Date: 04.04.12 Time: 15:35
 */

@XStreamAlias( "SingleTopicData" )
public class SingleTopicData implements Serializable
{
    @XStreamAsAttribute
    private String id;

    @XStreamAsAttribute
    private String forumId;

    @XStreamAsAttribute
    private boolean othersMark;

    @XStreamAsAttribute
    private boolean mvpMark;

    @XStreamAsAttribute
    private TopicType topicType;

    @XStreamAsAttribute
    private String topic;

    @XStreamAsAttribute
    private Integer replyCount;

    @XStreamAsAttribute
    private String updateDate;

    @XStreamAsAttribute
    private String userId;

    @XStreamImplicit( itemFieldName = "item" )
    private List<ItemData> parsedItems;

    public SingleTopicData ()
    {
        super ();
    }

    public String getId ()
    {
        if ( id == null )
        {
            id = TextUtils.generateId ( "STD" );
        }
        return id;
    }

    public void setId ( String id )
    {
        this.id = id;
    }

    public String getForumId ()
    {
        return forumId;
    }

    public void setForumId ( String forumId )
    {
        this.forumId = forumId;
    }

    public boolean isOthersMark ()
    {
        return othersMark;
    }

    public void setOthersMark ( boolean othersMark )
    {
        this.othersMark = othersMark;
    }

    public boolean isMvpMark ()
    {
        return mvpMark;
    }

    public void setMvpMark ( boolean mvpMark )
    {
        this.mvpMark = mvpMark;
    }

    public TopicType getTopicType ()
    {
        return topicType;
    }

    public void setTopicType ( TopicType topicType )
    {
        this.topicType = topicType;
    }

    public String getTopic ()
    {
        return topic;
    }

    public void setTopic ( String topic )
    {
        this.topic = topic;
    }

    public List<ItemData> getParsedItems ()
    {
        return parsedItems;
    }

    public void setParsedItems ( List<ItemData> parsedItems )
    {
        this.parsedItems = parsedItems;
    }

    public Integer getReplyCount ()
    {
        return replyCount;
    }

    public void setReplyCount ( Integer replyCount )
    {
        this.replyCount = replyCount;
    }

    public String getUserId ()
    {
        return userId;
    }

    public void setUserId ( String userId )
    {
        this.userId = userId;
    }

    public String getUpdateDate ()
    {
        return updateDate;
    }

    public void setUpdateDate ( String updateDate )
    {
        this.updateDate = updateDate;
    }

    public String toString ()
    {
        return topic + " (" + userId + ", " + updateDate + ")";
    }

    public SingleTopicData clone ()
    {
        SingleTopicData cloned = new SingleTopicData ();
        cloned.setId ( getId () );
        cloned.setForumId ( getForumId () );
        cloned.setOthersMark ( isOthersMark () );
        cloned.setMvpMark ( isMvpMark () );
        cloned.setTopicType ( getTopicType () );
        cloned.setTopic ( getTopic () );
        cloned.setReplyCount ( getReplyCount () );
        cloned.setUpdateDate ( getUpdateDate () );
        cloned.setUserId ( getUserId () );

        List<ItemData> parsedItems = new ArrayList<ItemData> ();
        if ( getParsedItems () != null )
        {
            for ( ItemData parsedItem : getParsedItems () )
            {
                parsedItems.add ( parsedItem.clone () );
            }
        }
        cloned.setParsedItems ( parsedItems );

        return cloned;
    }

    public boolean equals ( SingleTopicData obj )
    {
        return getId ().equals ( obj.getId () );
    }

    /**
     * Дополнительные методы
     */

    public Icon getIcon ()
    {
        return getTopicType () != null ? getTopicType ().getIcon () : null;
    }

    public User getUser ()
    {
        return UsersManager.getUser ( userId );
    }

    public String getTopicLink ()
    {
        return ForumManager.forumAddress + "viewtopic.php?f=" + forumId + "&t=" + id;
    }

    public String getResponseLink ()
    {
        return ForumManager.forumAddress + "posting.php?mode=reply&f=" + forumId + "&t=" + id;
    }
}